package loja.controller.despesa;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import loja.model.despesa.Despesa;
import loja.services.factory.ServiceFactory;
import loja.services.factory.helper.ServiceFactoryHelper;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

@ManagedBean(name = "despesasChartRSBean")
@RequestScoped
public class DespesasChartRSBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private CartesianChartModel barChartModel;

	public DespesasChartRSBean() {
		createBarChartModel();
	}

	private void createBarChartModel() {

		barChartModel = new CartesianChartModel();

		ChartSeries valorTotalSeries = new ChartSeries();
		valorTotalSeries.setLabel("Valor Total");

		ChartSeries provisionadoSeries = new ChartSeries();
		provisionadoSeries.setLabel("Valor Provisionado");

		ServiceFactory serviceFactory = ServiceFactoryHelper
				.getServiceFactory();

		List<Despesa> despesas = serviceFactory.getDespesaService()
				.listarDespesa();

		for (Despesa despesa : despesas) {
			String label = despesa.getIdDespesa() + " - "
					+ despesa.getDescricao();
			valorTotalSeries.set(label, despesa.getValor());
			BigDecimal provisionado = serviceFactory.getTransacaoService()
					.getSaldoConta(despesa.getTitular());
			provisionadoSeries.set(label, provisionado);
		}

		barChartModel.addSeries(valorTotalSeries);
		barChartModel.addSeries(provisionadoSeries);
	}

	public CartesianChartModel getBarChartModel() {
		return barChartModel;
	}

}
